//
//  HYPreviewHistoryManager.m
//  XPlayer
//
//  Created by liguoqing on 2023/10/6.
//

#import "HYPreviewHistoryManager.h"
#import <HYUIKit/NavigationScrollLabel.h>
#import "HYHomeFileItemViewModel.h"
#import "HYHomeVideoModel.h"
#import "HYHomeFolderModel.h"
#import "HYMyCollectionModel.h"
#import <HYUtls/CommUtls+Time.h>
#import <HYUtls/CommUtls+NSInteger.h>

static HYVideoViewModel *viewModel = nil;
static HYPreviewHistoryModel *historyModel = nil;
static NSArray *historySource = nil;

@interface HYLearnHistoryView : UIView

@property (nonatomic, copy) MineLearnHistoryBlock block;

@property (nonatomic, strong) UIImageView *iconImage;

@property (nonatomic, strong) NavigationScrollLabel *titleLb;

@property (nonatomic, strong) UILabel *subTitleLb;

@property (nonatomic, strong) UIProgressView *progessView;

@end

@implementation HYLearnHistoryView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self configUI];
    }
    return self;
}

- (void)configUI
{
    self.backgroundColor = [CommUtls dynamicColorWithLightModeHex:@"#F5F5F5" darkModeHex:@"#30343A"];
    self.layer.cornerRadius = 8.0;
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowOpacity = 0.15f;//不透明度
    self.layer.shadowOffset = CGSizeMake(0, 0);//偏移距离
    
    UIImageView *videoPlay = [[UIImageView alloc]init];
    [videoPlay setImage:[UIImage imageNamed:@"icon_play_mode"]];
    [self addSubview:videoPlay];
    [videoPlay mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.centerY.equalTo(self);
        make.width.height.mas_equalTo(24);
    }];
    
    NavigationScrollLabel *titleLb = [NavigationScrollLabel new];
    titleLb.needScroll = YES;
    titleLb.textColor = APPTextThemeColor;
    titleLb.bgColor = kClearColor;
    titleLb.textAlignment = NSTextAlignmentLeft;
    titleLb.font = [CommUtls mediumPingFangFontWithSize:14];
    [self addSubview:titleLb];
    [titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.equalTo(videoPlay.mas_right).offset(10);
        make.right.mas_equalTo(-25);
    }];
    self.titleLb = titleLb;
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn setImage:[UIImage imageNamed:@"icon_close_mode"] forState:UIControlStateNormal];
    [self addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-2.5);
        make.top.equalTo(self).offset(2.5);
        make.width.height.mas_equalTo(24);
    }];
    
    @weakify(self);
    closeBtn.rac_command = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        [self removeFromSuperview];
        return [RACSignal empty];
    }];
    
    UILabel *subTitleLb = [UILabel new];
    subTitleLb.textColor = APPTextThemeColor;
    subTitleLb.font = [CommUtls regularPingFangFontWithSize:12];
    subTitleLb.lineBreakMode = NSLineBreakByTruncatingMiddle;
    [self addSubview:subTitleLb];
    [subTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLb.mas_left);
        make.top.equalTo(titleLb.mas_bottom).offset(2);
        make.right.mas_equalTo(-25);
    }];
    self.subTitleLb = subTitleLb;
    
    UIButton *playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:playBtn];
    [playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(videoPlay);
        make.top.right.mas_equalTo(titleLb);
        make.bottom.mas_equalTo(subTitleLb);
    }];
    playBtn.rac_command = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        if (self.block) {
            [HYPreviewHistoryManager fetchPlayVideoViewModel:historyModel];
            self.block(historyModel.fileType == HYHomeFileType_Audio,viewModel);
        }
        [self removeFromSuperview];
        return [RACSignal empty];
    }];
    
    UIProgressView *progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
    progressView.progressTintColor = APPMainThemeColor;
    progressView.trackTintColor = UIColor.clearColor;
    [self addSubview:progressView];
    [progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(2);
    }];
    self.progessView = progressView;
}

- (void)showHistoryViewinView:(UIView *)view jump:(MineLearnHistoryBlock)block
{
    self.block = block;
    if (historyModel) {
        NSInteger duration = [CommUtls hmsToTime:historyModel.videoDuration];
        CGFloat progress = historyModel.videoPlayTime*1./duration;
        self.titleLb.text = [NSString stringWithFormat:@"%@",historyModel.fileName];
        self.subTitleLb.text = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"播放记录", nil),[CommUtls timeToMinMS:@(historyModel.videoPlayTime)]];
        self.progessView.progress = progress;
    }
    
    [view addSubview:self];
    [self mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.bottom.mas_equalTo(-TABLE_BAR_HEIGHT-15);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(60);
    }];
    [self.titleLb reStartScroll];
}

- (void)updateHistoryView
{
    self.titleLb.text = [NSString stringWithFormat:@"%@",historyModel.fileName];
    self.subTitleLb.text = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"播放记录", nil),[CommUtls timeToMinMS:@(historyModel.videoPlayTime)]];
    [self.titleLb reStartScroll];
}

@end


@interface HYPreviewHistoryManager ()

@property (nonatomic, strong) HYLearnHistoryView *historyView;

@end

@implementation HYPreviewHistoryManager

+ (instancetype)sharedManager
{
    static HYPreviewHistoryManager *sharedManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManager = [[self alloc] init];
    });
    return sharedManager;
}

+ (void)showHistoryInView:(UIView *)view jump:(MineLearnHistoryBlock)block
{
    [[[self sharedManager] historyView] removeFromSuperview];
    historyModel = [HYPreviewHistoryModel getRecentlyReviewHistory];
    if(historyModel){
        if(!historyModel.fileName){
            [historyModel deleteToDB];
            [self showHistoryInView:view jump:block];
            return;
        }
        [[[self sharedManager] historyView] showHistoryViewinView:view jump:block];
    }
}

+ (void)updateLearnHistoy
{
    if(historyModel){
        historyModel = [HYPreviewHistoryModel getRecentlyReviewHistory];
        [[[self sharedManager] historyView] updateHistoryView];
    }
}

+ (void)fetchPlayVideoViewModel:(HYPreviewHistoryModel *)previewModel
{
    if([previewModel.folderIdentification isEqualToString:@"fileImport-vplayer"]){// 文件导入
        [self fetchFileImportVideoSource:previewModel];
    }else if([previewModel.folderIdentification isEqualToString:@"myPhoto-vplayer"] ||[previewModel.folderIdentification isEqualToString:@"URL-vplayer"] ||[previewModel.folderIdentification isEqualToString:@"baidu-vplayer"] ){// 相册视频、 百度云盘视频、直接通过URL播放
        [self fetchMyPhotoVideoSource:previewModel];
    }else{
        [self fetchFolderVideoSource:previewModel];
    }
}

#pragma mark - 文件导入
+ (void)fetchFileImportVideoSource:(HYPreviewHistoryModel *)model
{
    NSString *folderName = model.folderPath;
    NSString *folderPath = [[HYFileManager documentDirectory] stringByAppendingPathComponent:folderName?:@""];
    NSArray *files = [kFileManager contentsOfDirectoryAtPath:folderPath error:nil];
    NSMutableArray *videoSource = [NSMutableArray array];
    HYVideoModel *currentModel = [[HYVideoModel alloc]init];
    BOOL isDir = NO;
    for (NSString *fileName in files) {
        NSString *filePath = [folderPath stringByAppendingPathComponent:fileName];
        [kFileManager fileExistsAtPath:filePath isDirectory:(&isDir)];
        NSDate *createDate = [HYFileManager getSingalFileCreateDateWithFilePath:filePath];
        if(!isDir){
            HYHomeFileType fileType = [[HYFileManager shareManager] fetchFileTypeWithFileName:fileName];
            NSString *fileSize = [CommUtls getSize:@([HYFileManager getSingleFileSizeWithFilePath:filePath])];
            if(model.fileType == fileType){
                HYVideoModel *videoModel = [[HYVideoModel alloc]init];
                videoModel.videoTitle = fileName;
                videoModel.videoUrl = filePath;
                videoModel.videoDuration = [CommUtls fetchVideoDurationWithUrl:filePath];
                videoModel.createDate = [CommUtls encodeTimeYearMonthDay:createDate];
                videoModel.videoSize = fileSize;
                if([model.createDate isEqualToString:videoModel.createDate] && [model.videoDuration isEqualToString:videoModel.videoDuration]){
                    videoModel.isSelect = YES;
                    currentModel = videoModel;
                }
                if(model.fileType == HYHomeFileType_Audio){
                    videoModel.isAudio = YES;
                }else if(model.fileType == HYHomeFileType_Video){
                    videoModel.videoImage = [CommUtls fetchVideoCoverImageWithUrl:filePath atTime:1.0];
                }
                [videoSource addObject:videoModel];
            }
        }
    }
    HYVideoPlayerConfig *config = [[HYVideoPlayerConfig alloc]init];
    config.isBasePlayer = YES;
    config.currentVideoModel = currentModel;
    config.videoSoucres = videoSource;
    config.isAudioMode = model.fileType == HYHomeFileType_Audio?YES:NO;
    viewModel = [[HYVideoViewModel alloc]initWithFolderIdentification:model.folderIdentification isPrivate:model.isPrivate videoConfig:config];
}

#pragma mark - 我的相册
+ (void)fetchMyPhotoVideoSource:(HYPreviewHistoryModel *)model
{
    HYVideoModel *videoModel = [[HYVideoModel alloc]init];
    videoModel.videoTitle = model.fileName;
    videoModel.videoUrl = model.filePath;
    videoModel.createDate = model.createDate;
    videoModel.videoImage = [UIImage imageWithContentsOfFile:[HYFileManager videoImageSavePathWithFileName:model.videoImagePath]];
    videoModel.videoImagePath = model.videoImagePath;
    videoModel.videoDuration = model.videoDuration;
    videoModel.isAudio = model.fileType == HYHomeFileType_Audio?YES:NO;
    videoModel.isSelect = YES;
    HYVideoPlayerConfig *config = [[HYVideoPlayerConfig alloc]init];
    config.isBasePlayer = YES;
    config.currentVideoModel = videoModel;
    config.videoSoucres = @[videoModel];
    config.isAudioMode = videoModel.isAudio;
    viewModel = [[HYVideoViewModel alloc]initWithFolderIdentification:model.folderIdentification isPrivate:model.isPrivate videoConfig:config];
}

#pragma mark - 文件夹
+ (void)fetchFolderVideoSource:(HYPreviewHistoryModel *)previewModel
{
    BOOL isAudio = previewModel.fileType==HYHomeFileType_Audio?YES:NO;
    NSString *sql = [NSString stringWithFormat:@"select *from VIDEOLIST where folderIdentification = '%@' and isAudio = '%d' and isLock = '%d' and isSuperPrivate = '%d'",previewModel.folderIdentification,isAudio,NO,NO];
    NSArray *videoSource = [HYHomeVideoModel searchWithSQL:sql];
    if(!videoSource.count){
        viewModel = nil;
        return;
    }
    __block NSMutableArray *tempSource = [NSMutableArray array];
    __block HYVideoModel *currentModel = [[HYVideoModel alloc]init];
    [videoSource enumerateObjectsUsingBlock:^(HYHomeVideoModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
        HYVideoModel *videoModel = [[HYVideoModel alloc]init];
        videoModel.videoTitle = model.videoTitle;
        if([previewModel.filePath isEqualToString:model.videoUrl] || ([previewModel.createDate isEqualToString:model.createDate] && [previewModel.videoDuration isEqualToString:model.videoDuration])){
            videoModel.isSelect = YES;
            currentModel = videoModel;
        }
        if(model.isAudio){
            videoModel.videoUrl = model.videoUrl;
            videoModel.isAudio = YES;
            videoModel.isPrivate = model.isLock;
            videoModel.createDate = model.createDate;
            videoModel.videoSize = model.videoSize;
            videoModel.videoDuration = model.videoDuration;
            [tempSource insertObject:videoModel atIndex:0];
        }else{
            if(model.isLocal){
                videoModel.videoUrl = [HYFileManager fetchPhotoVideoImportPathWithVideoName:model.videoTitle];
            }else{
                videoModel.videoUrl = model.videoUrl;
            }
            videoModel.videoImage = [UIImage imageNamed:[HYFileManager videoImageSavePathWithFileName:model.imageName]];
            videoModel.videoImagePath = model.imageName;
            videoModel.isPrivate = model.isLock;
            videoModel.createDate = model.createDate;
            videoModel.videoSize = model.videoSize;
            videoModel.videoDuration = model.videoDuration;
            [tempSource insertObject:videoModel atIndex:0];
        }
    }];
    HYVideoPlayerConfig *config = [[HYVideoPlayerConfig alloc]init];
    config.isBasePlayer = YES;
    config.currentVideoModel = currentModel;
    config.videoSoucres = [tempSource copy];
    config.isAudioMode = currentModel.isAudio;
    viewModel = [[HYVideoViewModel alloc]initWithFolderIdentification:previewModel.folderIdentification isPrivate:previewModel.isPrivate videoConfig:config];
}

+ (void)hiddenLearnHistory
{
    historyModel = nil;
    [[[self sharedManager] historyView] removeFromSuperview];
}

- (HYLearnHistoryView *)historyView
{
    if (!_historyView) {
        _historyView = [HYLearnHistoryView new];
    }
    return _historyView;
}

#pragma mark - 最近播放
+ (NSArray *)fetchRecentPlayRecord
{
    if([HYMineSetManager shareManager].isAllowPrivateBrowse){
        return @[];
    }
    historySource = [HYPreviewHistoryModel fetchAllPreviewHistroyRecord];
    __block NSMutableArray *videoSource = [NSMutableArray array];
    [historySource enumerateObjectsUsingBlock:^(HYPreviewHistoryModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
        HYHomeFileItemViewModel *itemViewModel = [[HYHomeFileItemViewModel alloc]initWitHistroyModel:model];
        [videoSource addObject:itemViewModel];
    }];
    return [videoSource copy];
}

+ (void)playCurrentHistoryWithIndex:(NSInteger)index jump:(MineLearnHistoryBlock)block
{
    HYPreviewHistoryModel *historyModel = historySource[index];
    [self fetchPlayVideoViewModel:historyModel];
    if(block){
        block(historyModel.fileType == HYHomeFileType_Audio,viewModel);
    }
}

#pragma mark - 文件重命名
+ (void)modifyHistoryRecordVideoTitle:(NSString *)videoTitle videoModel:(HYHomeVideoModel *)videoModel
{
    NSString *sql = nil;
    if([videoModel.videoUrl containsString:@"http"]){
        sql = [NSString stringWithFormat:@"select * from HISTORY where fileName = '%@' and filePath = '%@'",videoTitle,videoModel.videoUrl];
    }else{
        sql = [NSString stringWithFormat:@"select * from HISTORY where fileName = '%@' and createDate = '%@'",videoTitle,videoModel.createDate];
    }
    HYPreviewHistoryModel *model = [HYPreviewHistoryModel searchWithSQL:sql].firstObject;
    if(model){
        model.fileName = videoModel.videoTitle;
        BOOL result = [model updateToDB];
        if(result){
            if(historyModel && model._id == historyModel._id){
                historyModel = [HYPreviewHistoryModel getRecentlyReviewHistory];
                [[HYPreviewHistoryManager sharedManager] historyView].titleLb.text = historyModel.fileName;
            }
            [kNotificationCenter postNotificationName:kHistroyRefreshListNotification object:nil];
            CLog(@"历史 - 文件重命名成功");
        }
    }
    
    NSString *collectSql = nil;
    if([videoModel.videoUrl containsString:@"http"]){
        collectSql = [NSString stringWithFormat:@"select * from COLLECT where fileName = '%@' and filePath = '%@'",videoTitle,videoModel.videoUrl];
    }else{
        collectSql = [NSString stringWithFormat:@"select * from COLLECT where fileName = '%@' and createDate = '%@'",videoTitle,videoModel.createDate];
    }
    HYMyCollectionModel *collectModel = [HYMyCollectionModel searchWithSQL:collectSql].firstObject;
    if(collectModel){
        collectModel.fileName = videoModel.videoTitle;
        BOOL result = [collectModel updateToDB];
        if(result){
            CLog(@"收藏 - 文件重命名成功");
        }
    }
}

#pragma mark - 首页移动文件夹
+ (void)updateHistoryRecordFolderIdentification:(NSString *)folderIdentification folderModel:(HYHomeFolderModel *)folderModel
{
    NSString *setSql = [NSString stringWithFormat:@"folderIdentification = '%@'",folderModel.folderIdentification];
    NSString *whereSql = [NSString stringWithFormat:@"folderIdentification = '%@'",folderIdentification];
    BOOL result = [HYPreviewHistoryModel updateToDBWithSet:setSql where:whereSql];
    if(result){
        CLog(@"历史 - 移动文件夹 - 成功");
    }
    BOOL collectResult = [HYMyCollectionModel updateToDBWithSet:setSql where:whereSql];
    if(collectResult){
        CLog(@"收藏 - 移动文件夹 - 成功");
    }
}

#pragma mark - 首页移动文件
+ (void)updateHistoryRecordFolderIdentification:(NSString *)folderIdentification videoModel:(HYHomeVideoModel *)videoModel
{
    NSString *sql = [NSString stringWithFormat:@"select *from HISTORY where fileName = '%@' and folderIdentification = '%@'",videoModel.videoTitle,folderIdentification];
    HYPreviewHistoryModel *model = [HYPreviewHistoryModel searchWithSQL:sql].firstObject;
    if(model){
        model.folderIdentification = videoModel.folderIdentification;
        BOOL result = [model updateToDB];
        if(result){
            CLog(@"历史 - 移动文件 - 成功");
        }
    }
    NSString *collectSql = [NSString stringWithFormat:@"select *from COLLECT where fileName = '%@' and folderIdentification = '%@'",videoModel.videoTitle,folderIdentification];
    HYMyCollectionModel *collectModel = [HYMyCollectionModel searchWithSQL:collectSql].firstObject;
    if(collectModel){
        collectModel.folderIdentification = videoModel.folderIdentification;
        BOOL result = [collectModel updateToDB];
        if(result){
            CLog(@"收藏 - 移动文件 - 成功");
        }
    }
}

#pragma mark - 沙盒移动文件夹
+ (void)updateHistoryRecordFolderPath:(NSString *)folderPath
                 folderIdentification:(NSString *)folderIdentification
                     newFolderPath:(NSString *)newFolderPath
{
    NSString *setSql = [NSString stringWithFormat:@"folderPath = '%@'",newFolderPath];
    NSString *whereSql = [NSString stringWithFormat:@"folderPath = '%@' and folderIdentification = '%@'",folderPath,folderIdentification];
    BOOL result = [HYPreviewHistoryModel updateToDBWithSet:setSql where:whereSql];
    if(result){
        CLog(@"沙盒移动文件夹 - 成功");
    }
    BOOL collectResult = [HYMyCollectionModel updateToDBWithSet:setSql where:whereSql];
    if(collectResult){
        CLog(@"收藏移动文件夹 - 成功");
    }
}
#pragma mark - 沙盒移动文件
+ (void)updateHistoryRecordFolderPath:(NSString *)folderPath
                          fileName:(NSString *)fileName
                     newFolderPath:(NSString *)newFolderPath
{
    NSString *sql = [NSString stringWithFormat:@"select *from HISTORY where fileName = '%@' and folderPath = '%@'",fileName,folderPath];
    HYPreviewHistoryModel *model = [HYPreviewHistoryModel searchWithSQL:sql].firstObject;
    if(model){
        model.folderPath = newFolderPath;
        BOOL result = [model updateToDB];
        if(result){
            CLog(@"历史 - 沙盒移动文件 - 成功");
        }
    }
    NSString *collectSql = [NSString stringWithFormat:@"select *from COLLECT where fileName = '%@' and folderPath = '%@'",fileName,folderPath];
    HYMyCollectionModel *collectModel = [HYMyCollectionModel searchWithSQL:collectSql].firstObject;
    if(collectModel){
        collectModel.folderPath = newFolderPath;
        BOOL result = [collectModel updateToDB];
        if(result){
            CLog(@"收藏 - 沙盒移动文件 - 成功");
        }
    }
}

/// 文件夹设置为私密
+ (void)setPrivateHistoryRecordWithFolderModel:(HYHomeFolderModel *)folderModel
{
    NSString *setSql = [NSString stringWithFormat:@"isPrivate = '%@'",@(folderModel.isLock)];
    NSString *whereSql = [NSString stringWithFormat:@"folderIdentification = '%@'",folderModel.folderIdentification];
    BOOL result = [HYPreviewHistoryModel updateToDBWithSet:setSql where:whereSql];
    if(result && folderModel.isLock){
        CLog(@"历史 - 文件夹设置私密 - 成功");
        if(historyModel && [folderModel.folderIdentification isEqualToString:historyModel.folderIdentification]){
            [self hiddenLearnHistory];
        }
    }
    BOOL collectResult = [HYMyCollectionModel updateToDBWithSet:setSql where:whereSql];
    if(collectResult){
        CLog(@"收藏 - 文件夹设置私密 - 成功");
    }
}

#pragma mark - 设置私密
+ (void)setPrivateHistoryRecordWithVideoModel:(HYHomeVideoModel *)videoModel
{
    NSString *sql = [NSString stringWithFormat:@"select *from HISTORY where fileName = '%@' and createDate = '%@'",videoModel.videoTitle,videoModel.createDate];
    HYPreviewHistoryModel *model = [HYPreviewHistoryModel searchWithSQL:sql].firstObject;
    if(model){
        model.isPrivate = videoModel.isLock;
        BOOL result = [model updateToDB];
        if(result){
            if(historyModel && model._id == historyModel._id){
                [self hiddenLearnHistory];
            }
            [kNotificationCenter postNotificationName:kHistroyRefreshListNotification object:nil];
        }
    }
    NSString *collectSql = [NSString stringWithFormat:@"select *from COLLECT where fileName = '%@' and createDate = '%@'",videoModel.videoTitle,videoModel.createDate];
    HYMyCollectionModel *collectModel = [HYMyCollectionModel searchWithSQL:collectSql].firstObject;
    if(collectModel){
        collectModel.isPrivate = videoModel.isLock;
        BOOL result = [collectModel updateToDB];
        if(result){
            CLog(@"收藏 - 当个文件设置私密 - 成功");
        }
    }
}

#pragma mark - 移除播放记录
+ (void)removeHistoryRecordWithFolderModel:(HYHomeFolderModel *)folderModel
{
    NSString *deleteSql = [NSString stringWithFormat:@"folderIdentification = '%@'",folderModel.folderIdentification];
    BOOL result =  [HYPreviewHistoryModel deleteWithWhere:deleteSql];
    if(result){
        if(historyModel && [historyModel.folderIdentification isEqualToString:folderModel.folderIdentification]){
            [self hiddenLearnHistory];
        }
        CLog(@"历史 - 首页移除文件夹 - 成功");
    }
    BOOL delete = [HYMyCollectionModel deleteWithWhere:deleteSql];
    if(delete){
        CLog(@"收藏 - 首页移除文件夹 - 成功");
    }
}

+ (void)removeHistoryRecordWithVideoModel:(HYHomeVideoModel *)videoModel
{
    NSString *sql = [NSString stringWithFormat:@"select *from HISTORY where fileName = '%@' and createDate = '%@'",videoModel.videoTitle,videoModel.createDate];
    HYPreviewHistoryModel *model = [HYPreviewHistoryModel searchWithSQL:sql].firstObject;
    if(model){
        BOOL result = [model deleteToDB];
        if(result){
            if(historyModel && model._id == historyModel._id){
                [self hiddenLearnHistory];
            }
        }
        CLog(@"历史 - 首页移除文件 - 成功");
    }
    NSString *collectSql = [NSString stringWithFormat:@"select *from COLLECT where fileName = '%@' and createDate = '%@'",videoModel.videoTitle,videoModel.createDate];
    HYMyCollectionModel *collectModel = [HYMyCollectionModel searchWithSQL:collectSql].firstObject;
    if(collectModel){
        BOOL result = [collectModel deleteToDB];
        if(result){
            CLog(@"收藏 - 首页移除文件 - 成功");
        }
    }
}

// 移除沙盒下文件夹
+ (void)removeDocumentHistoryRecordWithFolderPath:(NSString *)folderPath
                             folderIdentification:(NSString *)folderIdentification
{
    BOOL result = [HYPreviewHistoryModel deleteWithWhere:@"folderPath = '%@' and folderIdentification = '%@'",folderPath,folderIdentification];
    if(result){
        if(historyModel && [historyModel.folderPath isEqualToString:folderPath]){
            [self hiddenLearnHistory];
        }
        CLog(@"历史 - 删除文件夹 - 成功");
    }
    BOOL delete = [HYMyCollectionModel deleteWithWhere:@"folderPath = '%@' and folderIdentification = '%@'",folderPath,folderIdentification];
    if(delete){
        CLog(@"收藏 - 删除文件夹 - 成功");
    }
}

+ (void)removeHistoryRecordWithVideoTitle:(NSString *)videoTitle 
                               createDate:(NSString *)createDate
{
    NSString *sql = [NSString stringWithFormat:@"select *from HISTORY where fileName = '%@' and createDate = '%@'",videoTitle,createDate];
    HYPreviewHistoryModel *model = [HYPreviewHistoryModel searchWithSQL:sql].firstObject;
    if(model){
       BOOL result = [model deleteToDB];
        if(result){
            if(historyModel && model._id == historyModel._id){
                [self hiddenLearnHistory];
            }
            CLog(@"历史 - 删除文件 - 成功");
        }
    }
    NSString *collectSql = [NSString stringWithFormat:@"select *from COLLECT where fileName = '%@' and createDate = '%@'",videoTitle,createDate];
    HYMyCollectionModel *collectModel = [HYMyCollectionModel searchWithSQL:collectSql].firstObject;
    if(collectModel){
       BOOL result = [collectModel deleteToDB];
        if(result){
            CLog(@"收藏 - 删除文件 - 成功");
        }
    }
}

+ (void)removeCurrentHistoyWithIndex:(NSInteger)index resultBlock:(dispatch_block_t)resultBlock
{
    HYPreviewHistoryModel *model = historySource[index];
    BOOL isResult = [model deleteToDB];
    if(isResult){
        if(resultBlock){
            resultBlock();
        }
    }
}

+ (BOOL)clearAllPlayHistory
{
    BOOL isClear =  [HYPreviewHistoryModel deleteWithWhere:@"1"];
    if(isClear){
        [self hiddenLearnHistory];
    }
    return isClear;
}

@end
